package com.example.springbootdemo.demos.web.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.text.StyledEditorKit;
@RestController
@RequestMapping("/user")
public class UserController {
    //登录接口
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
//        if (userName == null || userName.length() == 0 || password == null || password.length() == 0){
//            return false;
//        }
        //校验参数的合法性
        //Spring提供了专门的接口方法来判断字符串是否有长度
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        //进行用户名和密码的校验
        if ("admin".equals(userName) && "admin".equals(password)){
            //如何匹配则创建Session
            session.setAttribute("userName","admin");
            return true;
        }
        return false;
    }
    //获取用户的登录信息
    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpServletRequest request){
        HttpSession session = request.getSession(false);//没有会话也不创建
        String userName = null;
        if (session != null){
            userName = (String) session.getAttribute("userName");
        }
        return userName;
    }
}
